package org.vg.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 数据库连接类
 * @author 韩进城
 *
 */
public class DateConnection {
	
	public static final String DBDRIVER="com.mysql.jdbc.Driver";
	
	public static final String DBURL="jdbc:mysql://localhost:3306/vg";
	
	public static final String USERNAME="vg";
	
	public static final String PASSWROD="vg";
	
	
	private Connection connection;
	//加载数据库驱动
	static{
		
		try {
			Class.forName(DBDRIVER);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 取得数据库连接,使用后自己关闭连接
	 * @return
	 */
	public Connection getConnection() throws Exception{
		
		if(this.connection==null){
			
			connection=DriverManager.getConnection(DBURL,USERNAME,PASSWROD);
			
		}
		
		return connection;
	}
	
	/**
	 * 关闭数据库所有资源
	 */
	public void closeConnection(Connection connection,ResultSet rs){
		
		try {
			if(connection!=null){
				connection.close();
			}
			
			if(rs!=null){
				rs.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
